<?php
require 'cabesario.php';
include 'model/Activity.php';
$activity = new Activity();
$type = 'adicionar';
if (isset($_SESSION["autenticacao"])) {
    if ($_SESSION['autenticacao'] == "ok") {
        if ($_POST != null) {
            if (isset($_POST['adicionar'])) {
                $activity->setUser($_SESSION['usuario']->getId());
                $activity->setDescription($_POST['ip_description']);
                $activity->setLocal($_POST['ip_local']);
                $activity->setData($_POST['ip_data']);
                $activity->setHora($_POST['ip_hora']);
                if ($activity->insert()) {
                    $_SESSION["message"] = array('type' => 'ok', 'message' => "Savo com sucesso!");
                    header("location: myactivity.php");
                } else {
                    echo '<div id="message" class="error">Problemas na insersão. Informe o problema ao administrador do sistema</div>';
                }
            } else if (isset($_POST['alterar'])) {
                $activity->setUser($_SESSION['usuario']->getId());
                $activity->setDescription($_POST['ip_description']);
                $activity->setLocal($_POST['ip_local']);
                $activity->setData($_POST['ip_data']);
                $activity->setHora($_POST['ip_hora']);
                $activity->setId($_POST['ip_id']);
                if ($activity->update()) {
                    $_SESSION["message"] = array('type' => 'ok', 'message' => "Atualizado com sucesso!");
                    header("location: myactivity.php");
                } else {
                    echo '<div id="message" class="error">Problemas na alteração. Informe o problema ao administrador do sistema</div>';
                }
            } else if (isset($_POST['deletar'])) {
                $activity->setId($_POST['deletar']);
                if ($activity->delete()) {
                    $_SESSION["message"] = array('type' => 'ok', 'message' => "Contato removido com sucesso!");
                    header("location: myactivity.php");
                } else {
                    echo '<div id="message" class="error">Problemas na alteração. Informe o problema ao administrador do sistema</div>';
                }
            }
        }
        if ($_GET != null) {
            if (isset($_GET['edit'])) {
                $type = 'alterar';
                $activity->selectById($_GET['edit']);
            }
        }
        ?>
        <form method="post" action="activity.php">
            <input type="hidden" name="ip_id" value="<?php echo $activity->getId() ?>"/>
            <input type="hidden" name="<?php echo $type ?>" />
            <input class="input_text" type="text" name="ip_description" required="true" placeholder="Descrição " value="<?php echo $activity->getDescription() ?>"/><br/>
            <input class="input_text" type="text" name="ip_local" required="true" placeholder="Local" value="<?php echo $activity->getLocal() ?>"/><br/>
            <input class="input_text" type="date" name="ip_data" required="true" placeholder="Data" value="<?php echo $activity->getData() ?>"/><br/>
            <input class="input_text" type="time" name="ip_hora" required="true" placeholder="Horas" value="<?php echo $activity->getHora() ?>"/><br/>
            <br/><hr/><input class="style_button" type="submit" value="Salvar">
        </form>
        <?php if ($type == "alterar") { ?>
            <form method="post" action="activity.php" >
                <input type="hidden"  name="deletar" value="<?php echo $activity->getId(); ?>" />
                <button class="style_button" type="submit" onclick="return confirm('deletar?')">Deletar</button>
                <a class="style_button" href="myactivity.php">Voltar</a>
            </form>

        <?php } ?>
        <?php
    }
} else {
    header("location: index.php");
}require 'rodape.php';
?>
<script src="js/jquery.maskedinput.js"></script> 